/**
 * 
 */
package dbexel.presentation.aat.bis;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static org.hamcrest.Matchers.*;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ConfirmHandler;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;

import dbexel.model.dao.JPATestTools;
import dbexel.model.dao.WorkSheetDao;
import dbexel.model.dao.WorkSheetDaoImpl;
import dbexel.model.mapping.WorkSheet;
import dbexel.system.tools.DBExelHtmlUnitSupport;
import dbexel.system.tools.DBExelTestTools;
import dbexel.system.tools.FileUtils;

import static org.junit.Assert.*;

/**
 * @author gandomi
 * 
 */
public class ATestWorkSheetAttributeManipulations {

	private WebClient webClient;
	private WorkSheet f_ws;

	private URL startPageUrl;

	@BeforeClass
	public static void cleanUp() {
		JPATestTools.cleanAllData();
	}

	@Before
	public void initSystem() throws MalformedURLException {
		webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
		webClient.setJavaScriptEnabled(true);
		webClient.getCookieManager().setCookiesEnabled(true);

		f_ws = DBExelTestTools.fetchTestWorkSheet("AddAttrValue WorkSheet");
		WorkSheetDao l_wsDao = new WorkSheetDaoImpl();
		l_wsDao.createWorkSheet(f_ws);

		startPageUrl = new URL(FileUtils.fetchProperty("dbexel.testsite")
				+ "/DBEXEL/ShowDistinctWorkSheet.action?workSheet_Id="
				+ f_ws.getWs_Id());
	}

	@After
	public void cleanWindows() {
		JPATestTools.cleanAllData();
		webClient.closeAllWindows();

	}

	// Test flow
	// We move from the worksheet detail page
	@Test
	public void deleteAnAttribute() throws FailingHttpStatusCodeException,
			IOException {
		// 1. Test Ramp-Up
		HtmlPage l_currentPage = (HtmlPage) webClient.getPage(startPageUrl);

		// l_currentPage.get
		// 2. Validate Starting Point
		assertThat("Are we starting in the right page?",
				DBExelHtmlUnitSupport.getMetaContentByName(l_currentPage,
						"pageName"), equalTo("WorkSheet Detail Page"));

		// 3. Take References
		// Get the page name
		String l_refStartPageTitle = l_currentPage.getTitleText();

		int l_lastDataColumnIdx = DBExelHtmlUnitSupport
				.getTableById(l_currentPage, "Entry_Table").getRow(0)
				.getCells().size() - 2;

		// Make a generic test item
		int l_testCellIdx = l_lastDataColumnIdx > 0 ? (l_lastDataColumnIdx - 1)
				: 0;

		HtmlAnchor l_attributeLink = (HtmlAnchor) DBExelHtmlUnitSupport
				.getTableById(l_currentPage, "Entry_Table").getRow(0)
				.getCell(l_testCellIdx).getFirstChild();

		String l_attributeName = l_attributeLink.getTextContent();

		String l_attributeValue = DBExelHtmlUnitSupport
				.getTableById(l_currentPage, "Entry_Table").getRow(1)
				.getCell(l_testCellIdx).getTextContent();

		int l_nrOfRows = DBExelHtmlUnitSupport.getTableById(l_currentPage,
				"Entry_Table").getRowCount();

		int l_nrOfColumns = DBExelHtmlUnitSupport
				.getTableById(l_currentPage, "Entry_Table").getRow(0)
				.getCells().size();

		WebAssert.assertLinkPresentWithText(l_currentPage,
				"Edit this Work Sheet");

		// 4. Initiate Tested Action
		l_currentPage = ((HtmlAnchor) l_currentPage.getElementById("editWS"))
				.click();

		// 5. Make Assertions
		assertThat("We should now be in the Attribute Detail Page",
				DBExelHtmlUnitSupport.getMetaContentByName(l_currentPage,
						"pageName"), is(equalTo("Update WorkSheet")));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(1)
						.getCell(l_testCellIdx).getFirstChild()
						.getTextContent(),
				equalTo(l_attributeLink.getTextContent()));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(2)
						.getCell(l_testCellIdx).getTextContent(),
				equalTo(l_attributeValue));

		// 6. Make reiterations

		webClient.setConfirmHandler(new ConfirmHandler() {
			public boolean handleConfirm(Page page, String message) {
				return true;
			}

		});

		// 4. Initiate Tested Action
		l_currentPage = ((HtmlAnchor) l_currentPage
				.getElementById("deleteAttribute_1")).click();

		assertThat("We should still be in the Attribute Detail Page",
				DBExelHtmlUnitSupport.getMetaContentByName(l_currentPage,
						"pageName"), is(equalTo("Update WorkSheet")));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(1)
						.getCell(l_testCellIdx).getFirstChild().getTextContent(),
				not(equalTo(l_attributeLink.getTextContent())));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(1)
						.getCell(l_testCellIdx).getTextContent(),
				not(equalTo(l_attributeValue)));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table")
						.getRowCount(), equalTo(l_nrOfRows + 1));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(0)
						.getCells().size(), equalTo(l_nrOfColumns - 2));

		// 5. Make Assertions

		l_currentPage = ((HtmlSubmitInput) l_currentPage
				.getElementById("backToWorkSheetDetail")).click();

		assertThat("We should now be back in the Worksheet Detail Page",
				l_currentPage.getTitleText(), is(equalTo(l_refStartPageTitle)));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(0)
						.getCell(l_testCellIdx).getFirstChild().toString(),
				not(equalTo(l_attributeLink.toString())));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(1)
						.getCell(l_testCellIdx).getTextContent(),
				not(equalTo(l_attributeValue)));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table")
						.getRowCount(), equalTo(l_nrOfRows));

		assertThat(
				DBExelHtmlUnitSupport
						.getTableById(l_currentPage, "Entry_Table").getRow(0)
						.getCells().size(), equalTo(l_nrOfColumns - 1));

		// 7. Test Ramp-Down
		
	}
}
